package com.booking.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.management.Query;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.booking.bean.Sellerusers;
import com.booking.bean.Users;
import com.booking.dao.UserDAO;

@Repository
public class UserDAOImpl implements UserDAO {
	
	//依赖注入SessionFactory
//	ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//	 SessionFactory sessionFactory = (SessionFactory) ctx.getBean(LocalSessionFactoryBean.class);
//	
	@Autowired
	private SessionFactory sessionFactory;
	//获取和当前线程绑定的 Session. 
	public Session getSession(){
		return sessionFactory.getCurrentSession();
	}
	
	
	@Transactional
	@Override
	public String createUser(Users user) {
		String result = null;
		String hql="from Users u where u.username = ?";
		org.hibernate.Query query = this.getSession().createQuery(hql); 
		
		query.setString(0,user.getUsername());
		List list = query.list();
		
		if(!(list.size()>0))
			result =  String.valueOf(this.getSession().save(user)) ;
		
		return result;
	}
	
	@Transactional
	@Override
	public Users queryUser(Users user) {
		String hql = "from Users u where u.username = ?  and u.password = ?";
		
		org.hibernate.Query query= this.getSession().createQuery(hql);
		
		query.setString(0,user.getUsername());
		query.setString(1,user.getPassword());
				
		List<Users> list = query.list();
		
	//	this.getSession().close();
		if(list.size()>0)
			return list.get(0);
		return null;
	}

	@Transactional
	@Override
	public Users updateUser(Users user) {
		
		String hql = "Update Users u set u.phone = '" + user.getPhone() + "',u.email = '" + user.getEmail() + "', u.password = " + user.getPassword() + ""
				+ " where u.id = " + user.getId() + "  ";
		

		/*org.hibernate.Query query= this.getSession().createQuery(hql);
		
		query.setString(0,user.getUsername());
		List<Users> list = query.list();
		if(list.size()>0)
			return list.get(0);
		else{*/
	    this.getSession().createQuery(hql).executeUpdate();
		//}
		return user;
	}


	@Transactional
	public Sellerusers queryUser(Sellerusers user) {
		String hql = "from Sellerusers u where u.username = ?  and u.password = ?";
		
		org.hibernate.Query query= this.getSession().createQuery(hql);
		
		query.setString(0,user.getUsername());
		query.setString(1,user.getPassword());
				
		List<Sellerusers> list = query.list();
		
	//	this.getSession().close();
		if(list.size()>0)
			return list.get(0);
		return null;
	}
}
